<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
class D
{
	protected $CI;

	public function __construct()
	{
		$this->CI = &get_instance();
	}

	public function find($field = '', $tablename, $where = '', $order_by = '')
	{
		if ($where) {
			$this->CI->db->where($where);
		}

		if ($order_by) {
			$this->CI->db->order_by($order_by);
		}

		if ($field) {
			$this->CI->db->select($field);
		}

		$row = $this->CI->db->get($tablename)->unbuffered_row('array');
		return $row[$field];
	}

	public function find1($a)
	{
		if (isset($a['where'])) {
			$this->CI->db->where($a['where']);
		}

		if (isset($a['order'])) {
			$this->CI->db->order_by($a['order']);
		}

		return $this->CI->db->get($a['table'])->unbuffered_row('array');
	}

	public function field($a)
	{
		if (isset($a['where']) && $a['where']) {
			$this->CI->db->where($a['where']);
		}

		if (isset($a['order_by']) && $a['order_by']) {
			$this->CI->db->order_by($a['order_by']);
		}

		$row = $this->CI->db->select($a['field'])->get($a['table'])->unbuffered_row('array');
		return $row[$a['field']];
	}

	public function rows($a)
	{
		if (isset($a['select'])) {
			$this->CI->db->select($a['select']);
		}

		if (isset($a['where'])) {
			$this->CI->db->where($a['where']);
		}

		if (isset($a['order'])) {
			$this->CI->db->order_by($a['order']);
		}

		if (isset($a['limit'])) {
			$this->CI->db->limit($a['limit']);
		}

		$rs = $this->CI->db->get($a['table']);
		$list = array();

		while ($row = $rs->unbuffered_row('array')) {
			$list[] = $row;
		}

		return $list;
	}

	public function sum($field = '', $tablename, $where = '')
	{
		if ($where) {
			$this->CI->db->where($where);
		}

		if ($field) {
			$this->CI->db->select('sum(' . $field . ') as total');
		}

		$row = $this->CI->db->get($tablename)->unbuffered_row('array');
		return num_s($row['total']);
	}

	public function group_count($field = '', $tablename, $where = '')
	{
		if ($where) {
			$this->CI->db->where($where);
		}

		return num_s($this->CI->db->group_by($field)->count_all_results($tablename));
	}

	public function last_price($where, $time = '')
	{
		if ($time == '24h') {
			$start = time() - (24 * 60 * 60);
			$where .= ' and add_time > ' . $start;
		}

		$row = $this->CI->db->where($where)->order_by('add_time desc')->get('deal')->unbuffered_row('array');
		return num_s($row['price'], 2);
	}

	public function high_price($where, $time = '')
	{
		if ($time == '24h') {
			$start = time() - (24 * 60 * 60);
			$where .= ' and add_time > ' . $start;
		}

		$row = $this->CI->db->where($where)->order_by('price desc')->get('deal')->unbuffered_row('array');
		return num_s($row['price'], 2);
	}

	public function low_price($where, $time = '')
	{
		if ($time == '24h') {
			$start = time() - (24 * 60 * 60);
			$where .= ' and add_time > ' . $start;
		}

		$row = $this->CI->db->where($where)->order_by('price asc')->get('deal')->unbuffered_row('array');
		return num_s($row['price'], 2);
	}

	public function volume($where, $time = '')
	{
		if ($time == '24h') {
			$start = time() - (24 * 60 * 60);
			$where .= ' and add_time > ' . $start;
		}

		$row = $this->CI->db->select('sum(num) as volume')->where($where)->get('deal')->unbuffered_row('array');
		return num_s($row['volume'], 4);
	}

	public function amount($where, $time = '')
	{
		if ($time == '24h') {
			$start = time() - (24 * 60 * 60);
			$where .= ' and add_time > ' . $start;
		}

		$row = $this->CI->db->select('sum(num*price) as amount')->where($where)->get('deal')->unbuffered_row('array');
		return num_s($row['amount'], 4);
	}

	public function range($where, $time = '')
	{
		if ($time == '24h') {
			$start = time() - (24 * 60 * 60);
			$where .= ' and add_time > ' . $start;
		}

		$row = $this->CI->db->where($where)->order_by('add_time desc')->get('deal');
		$first = $row->first_row('array');
		$next = $row->next_row('array');
		$per = ($next['price'] ? ($first['price'] - ($next['price'] / $next['price'])) * 100 : 0);
		return fnum($per, 4);
	}
}

defined('BASEPATH') || exit('No direct script access allowed');

?>
